package com.ydd.moudle.me;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.taofengsdk.okhttp.listener.DisposeDataListener;
import com.ydd.R;
import com.ydd.base.BaseActivity;
import com.ydd.constans.Constans;
import com.ydd.model.event.QuitLoginEvent;
import com.ydd.model.update.UpdateModel;
import com.ydd.network.http.RequestCenter;
import com.ydd.network.mina.MinaService;
import com.ydd.service.update.UpdateService;
import com.ydd.share.ShareManager;
import com.ydd.util.Util;
import com.ydd.util.db.SPManager;
import com.ydd.util.user.UserManager;
import com.ydd.widegt.CommonDialog;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by tao on 17-3-22.
 */

public class SettingActivity extends BaseActivity implements View.OnClickListener {
    /**
     * UI
     */
    private TextView mUpdateView;
    private TextView mQuitLoginView;

    private boolean isLogin;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_layout);

        initView();
        showLoginStatus();
    }

    private void initView() {
        initHeaderView(getString(R.string.setting), true);

        mUpdateView = (TextView) findViewById(R.id.update_view);
        mQuitLoginView = (TextView) findViewById(R.id.quit_login_button);

        mQuitLoginView.setOnClickListener(this);
        mUpdateView.setOnClickListener(this);

    }

    private void showLoginStatus() {
        isLogin = SPManager.getBoolean(SPManager.IS_LOGIN_USER, false);
        if (!isLogin) {
            mQuitLoginView.setVisibility(View.GONE);
        } else {
            mQuitLoginView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.update_view:
                if (hasPermission(Constans.WRITE_READ_EXTERNAL_PERMISSION)) {
                    checkVerison();
                } else {
                    requetPermission(Constans.WRITE_READ_EXTERNAL_CODE,
                            Constans.WRITE_READ_EXTERNAL_PERMISSION);
                }
                break;
            case R.id.quit_login_button:
                if (isLogin) {
                    quitLogin();
                }
                break;
            default:
                break;
        }
    }

    private void quitLogin() {
        SPManager.getInstance().putBoolean(SPManager.IS_LOGIN_USER, false);
        SPManager.getInstance().putBoolean(SPManager.AUTOMATIC__LOGIN_USER, false);
        ShareManager.getInstance().removeAccount();
        UserManager.getInstance().setUser(null);
        mQuitLoginView.setVisibility(View.GONE);
        stopService(new Intent(SettingActivity.this, MinaService.class));
        EventBus.getDefault().post(new QuitLoginEvent());
    }

    private void checkVerison() {
        RequestCenter.requestCheckVersion(new DisposeDataListener() {
            @Override
            public void onSuccess(Object responseObj) {
                UpdateModel updateModel = (UpdateModel) responseObj;
                if (Util.getVersionCode(getApplicationContext()) < updateModel.getData().getCurrentVersion()) {
                    showCommonDialog();
                } else {
                    Toast.makeText(getApplicationContext(), R.string.is_new_version, Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Object reasonObj) {
                Log.e(TAG, reasonObj.toString());
            }
        });
    }

    private void showCommonDialog() {
        CommonDialog dialog = new CommonDialog(this,
                getString(R.string.update_new_version),
                getString(R.string.update_title), getString(R.string.update_install),
                getString(R.string.cancel), new CommonDialog.DialogClickListener() {
            @Override
            public void onDialogClick() {
                Intent intent = new Intent(getApplicationContext(), UpdateService.class);
                startService(intent);
            }
        });
        dialog.show();
    }

    @Override
    public void doSDWritePermission() {
        checkVerison();
    }
}
